<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>学生信息导入</title>

    <link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" type="text/css"
          href="../../static/plugin/easyui/themes/default/easyui.css"
          th:href="@{/plugin/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" href="../../static/plugin/easyui/themes/icon.css"
          th:href="@{/plugin/easyui/themes/icon.css}">

    <link rel="stylesheet" type="text/css" href="../../static/css/style_index.css"
          th:href="@{/css/style_index.css}">
    <link rel="stylesheet" type="text/css" href="../../static/css/style_table.css"
          th:href="@{/css/style_table.css}">
    <link rel="stylesheet" type="text/css" href="../../static/plugin/layui/css/layui.css"
          th:href="@{/plugin/layui/css/layui.css}">
    <script type="text/javascript" src="../../static/js/jquery.min.js"
            th:src="@{/js/jquery.min.js}"></script>
    <script type="text/javascript" src="../../static/plugin/layui/layui.all.js"
            th:src="@{/plugin/layui/layui.all.js}"></script>
    <style type="text/css">
        button{
            font-size:12px;
        }
    </style>
</head>
<body>
<div id="dlg" align="center">
    <form id="batchUpload" action="addressResourceInput/upload" method="post"
          enctype="multipart/form-data" class="form-horizontal"
          style="margin-top: 20px;margin-bottom: 20px;">
        <button class="btn btn-info" id="uploadEventBtn"
                style="margin-left: -5px;height: 30px;width: 79px;" type="button">
            选择文件
        </button>
        <input type="file" name="file" style="width:0px;height:0px;display: none"
               id="uploadEventFile">
        <input id="uploadEventPath" disabled="disabled" type="text" placeholder="请选择excel表"
               style="border: 1px solid #e6e6e6; height: 26px;width: 200px;margin-left: 10px;"><br><br>
        <span style="margin-left: 0px;">批次名称：</span>
        <input type="text" id="batch_name" name="batch_name" placeholder="请填入批次名称"
               style="border: 1px solid #bbb; height: 30px;width: 200px;margin-left: 12px;"
               maxlength="20">
    </form>

    <button class="btn btn-info"  onclick="downLoad()"
            style="margin-left: 0px;height: 30px;width: 80px;">下载模板
    </button>
    <button type="button" id="upload" class="btn btn-info" onclick="user.uploadBtn()"
            style="height: 30px;width: 57px;">上传
    </button>
    <button id="hide" class="btn btn-warning" type="button" onclick="cancel()"
            style="height: 30px;width: 57px;">关闭
    </button>

</div>
</body>

<script>
    var user;
    $(function () {
        user = new User();
        user.init();
    });

    var User = function () {
        this.init = function () {
            //模拟上传excel
            $("#uploadEventBtn").unbind("click").bind("click", function () {
                $("#uploadEventFile").click();
            });
            $("#uploadEventFile").bind("change", function () {
                $("#uploadEventPath").attr("value", $("#uploadEventFile").val());
            });
        };
        //点击上传按钮
        this.uploadBtn = function () {
            document.getElementById("upload").onclick = null;
            var batch_name = $("#batch_name").val();
            if (batch_name == '') {
                layer.msg("请输入批次号", {time: 1000});
                document.getElementById("upload").onclick = user.uploadBtn;
                return;
            }
            var uploadEventFile = $("#uploadEventFile").val();
            if (uploadEventFile == '') {
                layer.msg("请选择excel,再上传");
                document.getElementById("upload").onclick = user.uploadBtn;
                return;
            } else if (uploadEventFile.lastIndexOf(".xls") < 0) {//可判断以.xls和.xlsx结尾的excel
                layer.msg("只能上传Excel文件");
                document.getElementById("upload").onclick = user.uploadBtn;
                return;
            } else {
                var url = "/studentInput/upload";
                var formData = new FormData($('#batchUpload')[0]);
                user.sendAjaxRequest(url, 'POST', formData)
            }
        };
        this.sendAjaxRequest = function (url, type, data) {
            $.ajax({
                url: url,
                type: type,
                data: data,
                success: function (result) {
                    if (result.result > 0) {
                        parent.queryData(10,1);
                        parent.successClose("导入成功");

                    } else if (result.result == 0) {
                        layer.msg("excel上传失败", {time: 1000});
                        document.getElementById("upload").onclick = user.uploadBtn;
                    } else if (result.result == -1) {
                        layer.msg("excel数据为空", {time: 1000});
                        document.getElementById("upload").onclick = user.uploadBtn;
                    }

                },
                error: function () {
                    layer.msg("excel上传失败", {time: 1000});
                    document.getElementById("upload").onclick = user.uploadBtn;
                },
                cache: false,
                contentType: false,
                processData: false
            });
        };
    }

    /**
     * Description:下载Excel模板
     */
    function downLoad() {
        var url = "/studentInput/downLoad";
        url = encodeURI(url);
        location.href = url;
    }

    function cancel() {
        parent.closeWindow();
    }
</script>

</html>